package com.rangers.learning.psql.example;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Entity
@NoArgsConstructor
@TypeDef(name = "JsonbType", typeClass = JsonbType.class, parameters = {
		@Parameter(name = JsonbType.CLASS, value = "com.rangers.learning.psql.example.UserLabel") })
public class UserDetail {

	@Id
	@GeneratedValue
	private Long id;

	private String name;

	@Column(columnDefinition = "jsonb")
	@Type(type = "JsonbType")
	private UserLabel label;

	/**
	 * 未使用@Embeddable,序列化存储tinybob: {sex,age}
	 */
	private UserInfo userinfo;

	/**
	 * 使用了@Embeddable:平铺对象下的字段:{country,province,city} Tip:对象下的字段@Column等定义依然生效
	 */
	private UserAddress userAddress;

	/**
	 * 使用@Embedded：平铺对象下的字段: {name,catename}
	 * 
	 * @AttributeOverrides 覆盖对象中的字段：如：覆盖userHobby.name -> hobbyname
	 */
	@Embedded
	@AttributeOverrides({ @AttributeOverride(name = "name", column = @Column(name = "hobbyname")) })
	private UserHobby userHobby;

	public UserDetail(String name) {
		super();
		this.name = name;
	}

}
